import asyncio
import os

from dotenv import load_dotenv

from config import ACTIVE_SITE, SITE_CONFIGS, active_config
from crawlers.crawler_factory import CrawlerFactory

load_dotenv()


async def main():
    """
    入口点函数，根据配置创建并运行特定网站的爬虫
    """
    print(f"正在启动爬虫，目标网站: {ACTIVE_SITE}")
    
    try:
        # 使用爬虫工厂创建特定网站的爬虫实例
        crawler = CrawlerFactory.create_crawler(ACTIVE_SITE, active_config)
        
        # 运行爬虫
        await crawler.run()
        
        print(f"爬虫任务完成，网站: {ACTIVE_SITE}")
    except ValueError as e:
        print(f"错误: {e}")
        print(f"可用的网站配置: {', '.join(SITE_CONFIGS.keys())}")
        print("请设置环境变量 CRAWLER_ACTIVE_SITE 为上述网站之一，或添加新的网站配置")
    except Exception as e:
        print(f"爬虫运行时发生错误: {e}")


if __name__ == "__main__":
    asyncio.run(main())
